clear
use C:\Users\Lenovo\Desktop\研究数据.dta 
xtset citycode year
global xlist "gdp pop struc reve inv edu finc"
 global xlist "gdp struc reve inv finc"
*************************附录1.描述性统计*******************

asdoc sum entre policy usage_depth payment investment $xlist , dec(4), save(描述性统计.doc) title("描述性统计")

*************************一、基准回归 ********************

reg  entre policy
outreg2 using 基础回归.docx,replace tstat bdec(4) tdec(4) rdec(4) ctitle(entre) keep(policy) addtext(city fe, no,year fe, no)
xtreg  entre policy i.year,  fe robust
outreg2 using 基础回归.docx,append tstat bdec(4) tdec(4) rdec(4) ctitle(entre) keep(policy) addtext(city fe, yes,year fe, yes)
reg entre policy  $xlist  
outreg2 using 基础回归.docx,append tstat bdec(4) tdec(4) rdec(4) ctitle(entre) keep(policy $xlist) addtext(city fe, no,year fe, no)
xtreg entre policy $xlist i.year, fe robust
outreg2 using 基础回归.docx,append tstat bdec(4) tdec(4) rdec(4) ctitle(entre) keep(policy $xlist) addtext(city fe, yes,year fe, yes)

 ****************************二、平行趋势检验***********************
replace pd = 0 if pd > 10
 tab  pd
replace pd = 5 if pd > 5
replace pd = -5 if pd < -5

tab pd, gen(policyt)
forvalues i = 1/5{
 replace policyt`i' = 0 if policyt`i' == .
}
drop policyt6

reghdfe entre policyt* $xlist, abs(year city) cluster(provyear) keepsing

forvalues i = 1/5{
	gen b_`i' = _b[policyt`i']
}

gen avg_coef = (b_1 + b_2 + b_3 + b_4 + b_5)/5
 su avg_coef   //描述性统计
 return list   //获取执行结果并保存

coefplot, level(99) ///
   keep(policyt*)   ///
   coeflabels(policyt1 = "-5"   ///
   policyt2 = "-4"   ///
   policyt3 = "-3"             ///
   policyt4 = "-2"             ///
   policyt5 = "-1"              ///
   policyt6  = "0"             ///
   policyt7  = "1"             ///
   policyt8  = "2"             ///
   policyt9  = "3"             ///
   policyt10  = "4"            ///
   policyt11  = "5")           ///
   vertical                       ///
   yline(0)                       ///
   ytitle("回归系数")               ///
   xtitle("政策实施的相对时间") ///
   transform(*=@-r(mean)) ///
   addplot(line @b @at)                 ///
   ciopts(recast(rcap))                 ///
   scheme(s1mono)
graph export "article3_3.png",as(png) replace width(800) height(600)
 ****************************三、稳健性检验***********************
*****************************1.安慰剂检验**************************
///1.虚假的冲击时间变量安慰剂检验
//生成虚假的冲击时间变量
xtset citycode year
gen action2=reform-2 if treat==1
//生成虚假的解释变量
gen did2=(year>=action2)
//将解释变量代入基准回归
xtreg entre did2 $xlist i.year, fe cluster(citycode)
outreg2 using 安慰剂检验.doc,append tstat bdec(4) tdec(4) ctitle(2)

///2.多时点did安慰剂检验

use"C:\Users\Lenovo\Desktop\研究数据.dta",clear
//基准回归的真实系数 0.1306
mat b = J(500,1,0)
mat se = J(500,1,0) 
mat p = J(500,1,0) 
forvalues i=1/500{
qui use "C:\Users\Lenovo\Desktop\研究数据.dta  ",clear
qui xtset citycode year
qui keep if year==2014
qui sample 100, count 
qui keep citycode 
qui save 安慰剂.dta, replace 
qui merge 1:m citycode using C:\Users\Lenovo\Desktop\国家创业担保贷款贴息政策效果研究数据.dta  
qui replace treat = (_merge ==3)
qui replace after = (year>=2015)
qui  gen did = treat*after
 qui xtreg entre did gdp pop struc reve inv edu finc i.year, fe cluster(citycode)
 qui * 将回归结果赋值到对应矩阵的对应位置
 qui mat b[`i',1] = _b[did]
 qui mat se[`i',1] = _se[did]
 qui * 计算P值并赋值于矩阵
 qui mat p[`i',1] = 2*ttail(e(df_r), abs(_b[did]/_se[did]))
}
svmat b, names(coef)
svmat se, names(se)
svmat p, names(pvalue)

drop if pvalue1 == .
label var pvalue1 p值
label var coef1 估计系数
#delimit ;
twoway (kdensity coef1, yaxis(1) lc(black) title(安慰剂检验))
(scatter pvalue1 coef1, yaxis(2) m(Oh) mc(black)),
xline(0 0.1306,lc(black*0.5) lp(solid))
yline(0.01, lc(black*0.5) lp(dash) axis(2))
xtitle("估计系数",size(*0.8)) xlabel(-0.15(0.05)0.15) 
ytitle("Density", size(*0.8)) ylabel(, nogrid format(%4.1f))
ytitle("P值", size(*0.8) axis(2)) ///
ylabel(0(0.5)5,nogrid axis(1) format(%7.1f) angle(0)) ///
ylabel(,axis(2) angle(0))
legend(r(1) order(1 "核密度曲线" 2 "P Value")pos(6))
graphregion(color(white)) ;
#delimit cr
graph export"安慰剂检验．png",width(1000) replace

mean coef1 /／平均系数


****************************2.剔除直辖市**************************
use "C:\Users\Lenovo\Desktop\研究数据.dta ",clear
xtset citycode year
global xlist "gdp pop struc reve inv edu finc"
drop if citycode==110000 //剔除北京
drop if citycode==120000 //剔除天津
drop if citycode==310000 //剔除上海
drop if citycode==500000 //剔除重庆
xtreg entre policy $xlist i.year, fe robust
outreg2 using 剔除直辖市.docx,append tstat bdec(4) tdec(4) rdec(4) ctitle(entre) keep(policy $xlist) addtext(city fe, yes,year fe, yes)

*****************************3.替换被解释变量************************
use "C:\Users\Lenovo\Desktop\研究数据.dta ",clear
global xlist " struc inv finc"
reg newbegin policy  $xlist  
xtreg newbegin policy $xlist i.year, fe robust
outreg2 using 自雇率.docx,append tstat bdec(4) tdec(4) rdec(4) ctitle(newbegin) keep(policy  $xlist) addtext(city fe, yes,year fe, yes)
*****************************4.PSM-DID*****************************
use "C:\Users\Lenovo\Desktop\研究数据.dta ",clear
global xlist "gdp pop struc reve inv edu finc"
set seed 10101
psmatch2 treat  $xlist ,outcome(entre) n(1) ate ties logit common quietly
outreg2 using PSM-DID.docx,append tstat bdec(4) tdec(4) rdec(4) ctitle(entre) keep(policy $xlist) addtext(city fe, yes,year fe, yes)
pstest, both graph saving(balancing_assumption,replace)
graph export "balancing_assumption.emf",replace

 ****************************四、异质性分析***********************
twowayfeweights entre citycode year policy  $xlist ,type(feTR)  

reghdfe entre policy $xlist if rank==1,absorb(citycode year)vce(r)
outreg2 using 异质性城市等级.docx, append  tstat bdec(4) tdec(4) rdec(4) ctitle(entre) keep( policy) addtext(city fe, yes,year fe, yes)
reghdfe entre policy $xlist if rank==0, absorb(citycode year)vce(r)
outreg2 using 异质性城市等级.docx, append  tstat bdec(4) tdec(4) rdec(4) ctitle(entre) keep( policy) addtext(city fe, yes,year fe, yes)


reghdfe entre policy $xlist if line==1,absorb(citycode year)vce(r)
outreg2 using 异质性爱辉腾冲线.docx, append  tstat bdec(4) tdec(4) rdec(4) ctitle(entre) keep(policy) addtext(city fe, yes,year fe, yes)
reghdfe entre policy $xlist if line==0, absorb(citycode year)vce(r)
outreg2 using 异质性爱辉腾冲线.docx, append  tstat bdec(4) tdec(4) rdec(4) ctitle(entre) keep(policy) addtext(city fe, yes,year fe, yes)

 ****************************五、拓展分析***********************
*****************************1.中介效应**************************
*==usage_depth======

 Step 1：政策 → 信贷可得性（中介路径）
xtreg usage_depth policy $xlist i.year, fe robust
outreg2 using 中介3.docx, replace ///
    tstat bdec(4) tdec(4) rdec(4) ///
    ctitle(Policy -- usage_depth) ///
    keep(policy) addtext(city fe, yes, year fe, yes)
 Step 2：政策 + 中介 → 创业活力
xtreg entre policy usage_depth $xlist i.year, fe robust
outreg2 using 中介3.docx, append ///
    tstat bdec(4) tdec(4) rdec(4) ///
    ctitle(Policy + usage_depth -- Entrepreneurship) ///
    keep(policy usage_depth) addtext(city fe, yes, year fe, yes)
 
 *====payment======
 
 Step 1：政策 → 信贷可得性（中介路径）
xtreg payment policy $xlist i.year, fe robust
outreg2 using 中介2.docx, replace ///
    tstat bdec(4) tdec(4) rdec(4) ///
    ctitle(Policy -- payment) ///
    keep(policy) addtext(city fe, yes, year fe, yes)
 Step 2：政策 + 中介 → 创业活力
xtreg entre policy payment $xlist i.year, fe robust
outreg2 using 中介2.docx, append ///
    tstat bdec(4) tdec(4) rdec(4) ///
    ctitle(Policy + payment -- Entrepreneurship) ///
    keep(policy payment) addtext(city fe, yes, year fe, yes)
 
 
 *====investment======
 
 Step 1：政策 → 信贷可得性（中介路径）
xtreg investment policy $xlist i.year, fe robust
outreg2 using 中介1.docx, replace ///
    tstat bdec(4) tdec(4) rdec(4) ///
    ctitle(Policy -- investment) ///
    keep(policy) addtext(city fe, yes, year fe, yes)
 Step 2：政策 + 中介 → 创业活力
xtreg entre policy investment $xlist i.year, fe robust
outreg2 using 中介1.docx, append ///
    tstat bdec(4) tdec(4) rdec(4) ///
    ctitle(Policy + investment -- Entrepreneurship) ///
    keep(policy investment) addtext(city fe, yes, year fe, yes)

		
*==中介效应检验======
capture program drop med_bootstrap_multi
program define med_bootstrap_multi, rclass
    xtreg usage_depth policy $xlist i.year, fe
    scalar a1 = _b[policy]
    xtreg payment policy $xlist i.year, fe
    scalar a2 = _b[policy]
    xtreg investment policy $xlist i.year, fe
    scalar a3 = _b[policy]
    xtreg entre usage_depth payment investment policy $xlist i.year, fe
    scalar b1 = _b[usage_depth]
    scalar b2 = _b[payment]
    scalar b3 = _b[investment]
    scalar ab1 = a1 * b1
    scalar ab2 = a2 * b2
    scalar ab3 = a3 * b3
    scalar ab_total = ab1 + ab2 + ab3
    return scalar ab1 = ab1
    return scalar ab2 = ab2
    return scalar ab3 = ab3
    return scalar ab_total = ab_total

end
bootstrap r(ab1) r(ab2) r(ab3) r(ab_total), reps(1000) seed(1234): med_bootstrap_multi

	
	
**************************2.财政金融协调政策*************************
 eststo clear
eststo:  quietly reghdfe  entre np2017 cpn $xlist ,  a(city)
eststo:  quietly reghdfe  entre np2017 cpr $xlist ,  a(city)
eststo:  quietly reghdfe  entre np2017 cpe $xlist ,  a(city)
esttab, ar2 z scalars(df_m  ll chi2) star(* 0.10  ** 0.05 *** 0.01) nogap 
eststo:  quietly reghdfe  entre pp cpn $xlist ,  a(city) cluster(year)
eststo:  quietly reghdfe  entre pp cpr $xlist ,  a(city) cluster(year)
eststo:  quietly reghdfe  entre pp cpe $xlist ,  a(city) cluster(year)
esttab, ar2 z scalars(df_m  ll chi2) star(* 0.10  ** 0.05 *** 0.01) nogap 
eststo:  quietly reghdfe  entre np2017cpn $xlist ,  a(city)
eststo:  quietly reghdfe  entre np2017cpr $xlist ,  a(city) 
eststo:  quietly reghdfe  entre np2017cpe $xlist ,  a(city)
esttab, ar2 z scalars(df_m  ll chi2) star(* 0.10  ** 0.05 *** 0.01) nogap 
eststo:  quietly reghdfe  entre ppcpn $xlist ,  a(city) cluster(year)
eststo:  quietly reghdfe  entre ppcpr $xlist ,  a(city) cluster(year)
eststo:  quietly reghdfe  entre ppcpe $xlist ,  a(city) cluster(year)
esttab, ar2 z scalars(df_m  ll chi2) star(* 0.10  ** 0.05 *** 0.01) nogap 
 esttab using 拓展分析结果.rtf